package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.entity.Customer;
import com.igeek.collateral.mapper.CustomerMapper;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.CustomerService;
import com.igeek.collateral.vo.CustomerVo;
import com.igeek.collateral.vo.PageQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * TODO
 *
 * @author chh
 * @since 2024/6/27
 **/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    private CustomerMapper customerMapper;

    //分页查询
    @Transactional
    @Override
    public Page<Customer> findPage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Customer> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据姓名和性别)
        LambdaQueryWrapper<Customer> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasLength(queryString),Customer::getName,queryString)
                .or()
                .like(StringUtils.hasLength(queryString),Customer::getSex,queryString);
        //执行分页查询条件
        Page<Customer> pageC = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageC;
    }


    //根据id删除
    @Transactional
    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<Customer> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Customer::getId,id);
        int i=this.baseMapper.delete(lambdaQueryWrapper);
        return i>0?true:false;
    }


}
